<template>
  <div class="zy-content">
    <div class="searchBox search-box-layout">
      <Row :gutter="24">
        <Col span="4">
        <div>
          <p class="label">结案原因编码</p>
          <div class="ipt">
            <Input v-model="params.code" clearable :placeholder="`${$t('common.inputInfo')}`"
                   class="inputBorder">></Input>
          </div>
        </div>
        </Col>
        <Col span="4">
          <div>
            <p class="label">状态</p>
            <div class="ipt">
              <Select v-model="params.status">
                <Option :value="1">启用</Option>
                <Option :value="0">禁用</Option>
              </Select>
            </div>
          </div>
        </Col>
        <Col span="4">
        <div>
          <p class="label hide-label">--</p>
          <div class="ipt">
            <Button class="search-btn" type="primary" @click="changeNum()">{{ $t('common.Query') }}</Button>
            <Button class="reset-btn" @click="reset()" v-has="'dmsPurchaseList'">{{ $t('common.reset') }}
            </Button>
          </div>
        </div>
        </Col>
      </Row>
    </div>
    <div class="list-layout mt16">
      <div class="btns clear">
        <span class="title">{{ $t('common.listInformation') }}</span>
        <Button @click="addHandle">
          {{ $t('common.New') }}
        </Button>
<!--        <Button @click="importData">-->
<!--          {{ $t('common.Import') }}-->
<!--        </Button>-->
<!--        <Button @click="downloadTemplate">-->
<!--          {{ $t('common.Download') }}-->
<!--        </Button>-->

        <Button @click="getTableColumns(columns)">
          {{ handleContextMenuStatus ? $t('显示操作栏') : $t('隐藏操作栏') }}
        </Button>
      </div>
      <div class="tableContent">
        <Table ellipsis ref="selection" class="resizable-table" border @on-column-width-resize="onColumnWidthResize"
          :columns="columnsList" :data="list">
          <template slot-scope="{ row }" slot="action">
            <span class="operationBtn" @click="editRow(row)"><i class="iconfont zy-common-bianji"></i>{{ $t('common.Edit')
            }}</span>
            <span class="operationBtn" @click="delRow(row)"><i class="iconfont zy-common-shanchu"></i>{{
              $t('common.Delete') }}</span>
          </template>
        </Table>
      </div>
      <div class="pageWrap mt16">
        <pageLeftTitle :total="total" />
        <Page :total="total" :page-size="params.pageSize" :current="params.pageNo" @on-change="changePageNum"
          @on-page-size-change="changePageSize" show-sizer />
      </div>
    </div>
    <Modal :mask-closable="false" v-model="deleteShow" :title="$t('common.Delete')" class="modal-title-layout">
      <div class="icon red">
        <i class="iconfont zy-common-shanchu"></i>
      </div>
      <p class="title">
        <span>{{ $t('common.sureDelete') }}</span>
      </p>
      <div slot="footer">
        <Button type="default" @click="deleteShow = false">{{ $t('common.Cancle') }}</Button>
        <Button type="primary" @click="deleteSure">{{ $t('maintenance.Confirmed') }}</Button>
      </div>
    </Modal>

    <Modal :mask-closable="false" v-model="createStatus" :title="form.id ? $t('common.Edit') : $t('common.New')" class="modal-content-layout">
      <Form v-if="createStatus" class="formList" :model="form" label-position="top" ref="form" :rules="rules">
        <FormItem label="结案原因编码" required prop="code">
          <Input type="text" v-model="form.code" :placeholder="`${$t('common.inputInfo')}`" class="inputBorder"></Input>
        </FormItem>
        <FormItem label="结案原因名称" required prop="context">
          <Input type="text" v-model="form.context" :placeholder="`${$t('common.inputInfo')}`" class="inputBorder"></Input>
        </FormItem>
        <FormItem :label="'状态'">
          <Select v-model="form.status">
            <Option :value="1">启用</Option>
            <Option :value="0">禁用</Option>
          </Select>
        </FormItem>
      </Form>
      <div slot="footer" v-if="createStatus">
        <Button type="default" @click="createStatus = false">{{ $t('common.Cancle') }}</Button>
        <Button type="primary" @click="addHandleSubmit('form')">{{ $t('maintenance.Confirmed') }}</Button>
      </div>
    </Modal>

    <importExcelModal :importExcelStateF="importExcelState" v-if="importExcelState"
      @importExcelChange="importExcelChange"></importExcelModal>
  </div>
</template>

<script>
import ImportExcelModal from '@/components/importExcel.vue';
import fileDownload from "js-file-download";
import toggleTableOperation from "@/mixins/toggleTableOperation";
export default {
  name: "reasonClosure",
  components: { ImportExcelModal },
  mixins: [toggleTableOperation],
  data() {
    return {
      params: {
        pageSize: 20,
        pageNo: 1
      },
      // dictName: ['故障分类', '故障类型','故障现象', '设备类别', '备件分类', '维修工时单价', '技能类型', '技能', '维保达成率目标值', 'COT目标值', '维保成本单价','CAPEX FACTORY',],
      columns: [
        {
          title:'结案原因编码',
          key: 'code',
          align: 'center',
          resizable: true
        },
        {
          title: '结案原因名称',
          key: 'context',
          align: 'center',
          resizable: true
        },
        {
          title: '是否启用',
          key: 'status',
          align: 'center',
          resizable: true,
          render: (h, params) => {
            return h('span', params.row.status === 1 ? '启用' : '禁用')
          }
        },
        {
          title: `${this.$t('common.operation')}`,
          slot: "action",
          width: 200,
          align: "center",
          fixed: "right"
        }
      ],
      importExcelState: false,
      list: [],
      total: 0,
      deleteShow: false,
      currentItem: {},
      createStatus: false,
      form: {status: 1},
      nameList: [],
      rules: {
        code: [
          { required: true, message: this.$t('common.inputInfo'), trigger: "blur" },
        ],
        context: [
          { required: true, message: this.$t('common.inputInfo'), trigger: "blur" },
        ],
      }
    }
  },
  created() {
    this.getTableColumnsWidth(this.columns)
  },
  mounted() {
    this.getList()
  },
  methods: {
    handleRemote(query) {
      if (query) {
        this.axios({
          method: 'get',
          url: '/dms/dict/nameList',
          params: {
            name: query
          }
        }).then(res => {
          if (res.code === 200) {
            this.nameList = res.data
          } else {
            this.$Message.warning(({ content: res.msg, duration: 6, closable: true }));
          }
        })
      } else {
        // this.nameList = []
      }
    },
    importExcelChange(val) {
      this.importExcelState = false;
      if (val === 'success') {
        this.$Message.success('导入成功!')
        this.changeNum();
      }
    },
    getList() {
      this.axios({
        method: 'post',
        url: '/dms/basicEndReasons/list',
        data: {
          ...this.params
        }
      }).then(res => {
        if (res.code === 200) {
          this.list = res.data.list;
          this.total = res.data.total;
        } else {
          this.$Message.warning(({ content: res.msg, duration: 6, closable: true }));
        }
      })
    },
    reset() {
      this.params = {};
      this.changeNum();
    },
    changeNum() {
      this.params.pageNo = 1
      this.params.pageSize = 20
      this.getList()
    },
    addHandle() {
      this.form = {status:1}
      this.createStatus = true
    },
    downloadTemplate() {
      const filName = "数据字典导入模板";
      this.axios({
        method: "get",
        url: "/dms/dict/exportTemplate",
        responseType: "blob",
      }).then((res) => {
        fileDownload(res, filName + ".xlsx");
      });
    },
    importData() {
      this.importExcelState = true;
    },
    addHandleSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.axios({
            method: 'post',
            url: this.form.id ? '/dms/basicEndReasons/add' : '/dms/basicEndReasons/add',
            data: {
              ...this.form
            }
          }).then(res => {
            this.$Message.success(res.msg)
            this.currentItem = {};
            this.createStatus = false
            this.getList()
          })
        }
      })
    },
    editRow(row) {
      this.form = { ...row }
      this.createStatus = true
    },
    delRow(row) {
      this.currentItem = row;
      this.deleteShow = true
    },
    deleteSure() {
      this.axios({
        method: 'delete',
        url: '/dms/basicEndReasons/delete',
        data: [this.currentItem.id]
      }).then(res => {
        if (res.code === 200) {
          this.$Message.success(res.msg)
          this.currentItem = {};
          this.deleteShow = false
          this.getList()
        } else {
          this.$Message.warning(({ content: res.msg, duration: 6, closable: true }));
        }
      })
    },
    changePageNum(val) {
      this.params.pageNo = val
      this.getList()
    },
    changePageSize(val) {
      this.params.pageSize = val
      this.getList()
    }
  }
}
</script>

<style scoped></style>
